package manager;

import entity.Question;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import sessionBean.questionSessionBean;

@ManagedBean(name = "taskBean")
@ViewScoped
public class taskManagerBean {
    private List<Question> tasks = new ArrayList();
    private Question selectedTask;
    private Long taskId;
    private String taskTitle;
    private String taskInstruction;
    
    @EJB
    private questionSessionBean taskSession;
    
    @PostConstruct
    public void init() {
        System.out.println("TaskManagerBean Constructed");
        tasks = taskSession.getAllQuestions();
    }
    
    public void clearValue(){
        selectedTask = null;
        taskId = null;
        taskTitle = null;
        taskInstruction = null;
    }
    
    public void updateValue(){
        taskId = selectedTask.getId();
        taskTitle = selectedTask.getTitle();
        taskInstruction = selectedTask.getInstruction();
    }
    
    public void addTask(){
        try {
            System.out.println("addTask");
            taskId = taskSession.createQuestion(taskTitle, taskInstruction);
            System.out.println("Task added");
        } catch (Exception e) {
            System.err.println("Error occur during creation of new task");
        }
    }
    
    public void editTask(){
        try {
            taskSession.editQuestion(taskId, taskTitle, taskInstruction);
        } catch (Exception e) {
            System.err.println("Error occur during editting the selected task");
        }
    }
    
    public void deleteTask(){
        try {
            taskSession.deleteQuestion(taskId);
        } catch (Exception e) {
            System.err.println("Error occur during deleting  the selected task");
        }
    }

    public List<Question> getTasks() {
        return tasks;
    }

    public void setTasks(List<Question> tasks) {
        this.tasks = tasks;
    }

    public Question getSelectedTask() {
        return selectedTask;
    }

    public void setSelectedTask(Question selectedTask) {
        this.selectedTask = selectedTask;
    }

    public Long getTaskId() {
        return taskId;
    }

    public void setTaskId(Long taskId) {
        this.taskId = taskId;
    }

    public String getTaskTitle() {
        return taskTitle;
    }

    public void setTaskTitle(String taskTitle) {
        this.taskTitle = taskTitle;
    }

    public String getTaskInstruction() {
        return taskInstruction;
    }

    public void setTaskInstruction(String taskInstruction) {
        this.taskInstruction = taskInstruction;
    }


}
